<?php

set_time_limit(0);

header("Content-Type:text/plain");

$html = file_get_contents("http://www.abc.net.au/am");
//echo $html;

$contents = array();
if (preg_match_all('!<h4><a href="(.*?)">(.*?)</a></h4>!', $html, $links, PREG_SET_ORDER)) {
    foreach ($links as $m) {
        $c = &$contents[];
        $html = file_get_contents($m[1]); // link
        $c['title'] = preg_replace('/[:?!*\t\\\\\\/]/', '_', $m[2]); // title

        $pics = &$c['pics'];
        if (preg_match_all('!href="(.*?)" rel="extraimages"!', $html, $ms)) {
            foreach ($ms[1] as $m) {
                $pics[] = $m; // pics
            }
        }

        preg_match('!href="(.*?)"[^>]+id="fullstoryplayer"!', $html, $m);
        $c['mp3'] = $m[1]; // mp3

        preg_match('!<p class="body">(.*?)</p>!s', $html, $m);
        $c['html'] = $m[1];
        $c['article'] = preg_replace('!<br>!i', "\n", $m[1]); //article

        unset($c, $pics);
    }
}

$date = date('Y-m-d');
$dir = "news/" . date('Y-m-d');
if (!file_exists($dir)) {
    mkdir($dir, null, true);
}


$newsId = 1;
$panel = '';
$items = '';
foreach ($contents as $c) {
    $panel .= <<<html
   <div class="item" newsId="$newsId">
       $c[title]
   </div>
html;

    $items .= "<div class='itemContent' newsId='$newsId'><center>";
    $i = 0;
    foreach ((array) $c['pics'] as $pic) {
        if (trim($pic) != '') {
            if ($i == 0) {
                echo "copying $c[title].jpg\n";
                $des = "$dir/$c[title].jpg";
                $items .= "<img alt='$c[title]' src='$date/$c[title].jpg'/><br/>";
            } else {
                echo "copying $c[title]_$i.jpg\n";
                $des = "$dir/$c[title]_$i.jpg";
                $items .= "<img alt='$c[title]' src='$date/$c[title]_$i.jpg'/><br/>";
            }
            flush();
            copy($pic, $des);
//            echo "\n---- ", $pic, $des;
            $i++;
        }
    }
    $items .= <<<html
                           <OBJECT id='mediaPlayer1' width="180" height="50"
                                    classid='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'
                                    codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701'
                                    standby='Loading Microsoft Windows Media Player components...' type='application/x-oleobject'>
                                <param name='fileName' value="$date/$c[title].mp3">
                                <param name='animationatStart' value='true'>
                                <param name='transparentatStart' value='true'>
                                <param name='autoStart' value="false">
                                <param name='showControls' value="true">
                                <param name ="ShowAudioControls"value="true">
                                <param name="ShowStatusBar" value="true">
                                <param name='loop' value="false">
                                <EMBED type='application/x-mplayer2'
                                       pluginspage='http://microsoft.com/windows/mediaplayer/en/download/'
                                       id='mediaPlayer' name='mediaPlayer' displaysize='4' autosize='-1'
                                       bgcolor='darkblue' showcontrols="true" showtracker='-1'
                                       showdisplay='0' showstatusbar='-1' videoborder3d='-1' width="420" height="380"
                                       src="$date/$c[title].mp3" autostart="true" designtimesp='5311' loop="false">
                                </EMBED>
                            </OBJECT>
                            <br/>
                            <h2>$c[title]</h2>
                            <p class='article'>
                                $c[html]
                            </p>
                            </center>
                         </div>
html;

    echo "copying $c[title].mp3\n";
    copy($c['mp3'], "$dir/$c[title].mp3");
    flush();

    echo "putting content of '$c[title]'\n\n";
    flush();
    file_put_contents("$dir/$c[title].txt", $c['article']);

    $newsId++;
}

$web = <<<web
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="jquery-1.4.1.js"></script>
        <style>
            .panel {
                float: left;
                border: 1px #999 solid;
                margin: 10px 5px 10px 10px;
                padding: 8px 8px;
                width: 150px;
            }
            .panel .item{
                border: 1px #bbb solid;
                margin-bottom: 3px;
                padding: 2px 8px;
                cursor: pointer;
            }
            .content{
                float:right;
                border: 1px #999 solid;
                padding: 8px 8px;
                margin: 10px 10px 10px 4px;
                width: 560px;
            }
            .content .itemContent{
                display: none;
                margin-bottom: 20px;
            }
            .content .itemContent object{
                margin-bottom: 10px;
            }
            .content .itemContent img{
                margin-bottom: 10px;
                width: 400px;
            }
            .content .itemContent .article{
                text-align: left;
            }
            .body {
                width: 780px;
            }
        </style>
        <script type="text/javascript">
            $().ready(function(){
                $(".panel .item").mouseover(function(){
                    $(this).css("background-color", "#ccc");
                })
                .mouseout(function(){
                    $(this).css("background-color", "");
                })
                .click(function(){
                    display($(this).attr('newsId'))
                })
                display(1);
            })

            function display(newsId){
                $(".content .itemContent").hide();
                $(".content .itemContent[newsId="+newsId+"]").show();
            }
        </script>
    </head>
    <body>
        <center>
            <div class="body">
                <div class="panel">
                    $panel
                </div>
                <div class="content">
                    $items
                </div>
            </div>
        </center>
    </body>
</html>
web;

echo "\nGenerating news/$date.html";
file_put_contents("news/" . date('Y-m-d') . ".html", $web);